package com.example.wjh.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.wjh.entity.News;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface NewsMapper extends BaseMapper<News> {

    // 查询最新的三条资讯名称
    @Select("SELECT newsName FROM news ORDER BY id DESC LIMIT 3")
    List<News> findLatestNews();

    // 根据ID获取资讯
    @Select("SELECT * FROM news WHERE id = #{id}")
    News selectById(@Param("id") Long id);

    // 插入新的资讯
    @Insert("INSERT INTO news (publisherName, newsName, newsContent, newsSource, publishTime) " +
            "VALUES (#{publisherName}, #{newsName}, #{newsContent}, #{newsSource}, #{publishTime})")
    int insertNews(News news);

    // 更新资讯
    @Update("UPDATE news SET publisherName = #{publisherName}, newsName = #{newsName}, " +
            "newsContent = #{newsContent}, newsSource = #{newsSource}, publishTime = #{publishTime} " +
            "WHERE id = #{id}")
    int updateNews(News news);

    // 根据ID删除资讯
    @Delete("DELETE FROM news WHERE id = #{id}")
    int deleteById(@Param("id") Long id);

    // 获取所有资讯
    @Select("SELECT * FROM news")
    List<News> getAllNews();

    // 多条件查询
    @Select("<script> " +
            "SELECT * FROM news " +
            "<where> " +
            "<if test='publisherName != null'> AND publisherName LIKE CONCAT('%', #{publisherName}, '%')</if> " +
            "<if test='newsName != null'> AND newsName LIKE CONCAT('%', #{newsName}, '%')</if> " +
            "<if test='newsSource != null'> AND newsSource LIKE CONCAT('%', #{newsSource}, '%')</if> " +
            "</where> " +
            "</script>")
    List<News> searchNews(
            @Param("publisherName") String publisherName,
            @Param("newsName") String newsName,
            @Param("newsSource") String newsSource);
}

